﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Things.Common;
using Things.ThingsWeb;
using Things.Web.OrderPlan;
using Things.OrderPlan;

namespace Things.ThingsWeb.Components
{
    public partial class GoodsCartPlan : System.Web.UI.UserControl
    {
        public decimal TotalCount
        {
            get
            {
                return GetCount();
            }
        }
        Things.BLL.BuyerAddress bll = new Things.BLL.BuyerAddress();

        /// <summary>
        /// 所有总价
        /// </summary>
        public double TotalPrice { get; set; }

        private Dictionary<Guid, Things.OrderPlan.Goods> _goods = new Dictionary<Guid, Things.OrderPlan.Goods>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();

                string currentURL = Request.Url.ToString();
               
                GetCount();
            }
        }

        public void BindData()
        {
            DataTable dt = OrderPlanCartUtil.GetOrders(Context);
          
            if (dt == null || dt.Rows.Count == 0)
            {
                Panel1.Visible = true;
            }
            else
            {
                Panel1.Visible = false;
            }

            this._ctlCart.DataSource = dt;
            this._ctlCart.DataBind();
        }

        
        public decimal GetCount()
        {
            decimal totalCount = 0;
            int deliverdNumber = 0;

            Repeater rptCart = this._ctlCart as Repeater;
            if (rptCart != null)
            {
                RepeaterItemCollection items = rptCart.Items;
                foreach (RepeaterItem item in items)
                {
                    // 订购总价
                    Label lblTotalPrice = item.FindControl("lblTotalPrice") as Label;
                    if (lblTotalPrice != null)
                    {
                        if (decimal.TryParse(lblTotalPrice.Text.Trim(), out totalCount) == false)
                        {
                            totalCount = 0;
                        }
                    }
                    // 运送次数
                    Label lblDeliveredNumber = item.FindControl("lblDeliveredNumber") as Label;
                    if (lblDeliveredNumber != null)
                    {
                        if (int.TryParse(lblTotalPrice.Text.Trim(), out deliverdNumber) == false)
                        {
                            deliverdNumber = 0;
                        }
                    }

                    // 每次只能订购一种商品
                    break;
                }
            }

            // 商品价格
            this.lblCount.Text = totalCount.ToString();

            // 运费
            // 订购订单大于399元免运费
            decimal yunFei=0;
            if (totalCount >= ConfigureValue.DingGouMianYunFei) 
            {
                 yunFei= 0;
            }
            else
            {
                if (deliverdNumber > 0)
                {
                    yunFei = deliverdNumber * ConfigureValue.YunFei;
                }
                else
                {

                }
            }

            this.lblYunFei.Text=yunFei.ToString();

            // 订单价格
            this.lblDingDanJinE.Text = (totalCount + yunFei).ToString();

            return totalCount + yunFei;
        }


        protected string GetGoodsName(string strGoodsId)
        {
            Things.OrderPlan.Goods goods = null;
            Guid goodsId = new Guid(strGoodsId);
            if (!_goods.ContainsKey(goodsId))
            {
                goods = new Things.OrderPlan.PlanningGoodsSearcher().Find(goodsId);
                _goods.Add(goodsId, goods);
            }
            else
            {
                goods = _goods[goodsId];
            }
            return goods.GoodsName;
        }

        protected string GetSetName(string strSetId)
        {
            Guid setId = new Guid(strSetId);
            Things.OrderPlan.Models.SaleSet saleSet = new SaleSetRepository().Find(setId);
            if (saleSet == null)
            {
                return "自选套餐";
            }
            return saleSet.Caption;
        }

        protected string GetUnitPrice(string strSetId, string strGoodsId)
        {
            Guid setId = new Guid(strSetId);
            Things.OrderPlan.Models.SaleSet saleSet = new SaleSetRepository().Find(setId);
            if (saleSet == null)
            {
                Guid goodsId = new Guid(strGoodsId);
                Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
                if (parameter != null)
                {
                    decimal a = Math.Round((parameter.P1 - ((parameter.N * parameter.P1 - parameter.PN) / (parameter.N - 1))), 2);
                    decimal b = Math.Round(((parameter.N * parameter.P1 - parameter.PN) / (parameter.N - 1)), 2);

                    DataTable table = this._ctlCart.DataSource as DataTable;
                    foreach (DataRow row in table.Rows)
                    {
                        if (new Guid((string)row["SetId"]) == setId)
                        {
                            int dCount = Convert.ToInt32(row["deliveredCount"]);
                            int dCycle = Convert.ToInt32(row["deliveredCycle"]);
                            int dNumber = Convert.ToInt32(row["deliveredNumber"]);

                            return Math.Round(
                                dCount * dCycle * dNumber * a + b,
                                2).ToString();
                        }
                    }
                }
            }
            return saleSet.UnitPrice.ToString();
        }

        protected string GetTotalPrice(string strSetId, string strGoodsId)
        {
            Guid setId = new Guid(strSetId);
            DataTable table = this._ctlCart.DataSource as DataTable;

            Things.OrderPlan.Models.SaleSet saleSet = new SaleSetRepository().Find(setId);
            if (saleSet != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (new Guid((string)row["SetId"]) == setId)
                    {
                        int dCount = Convert.ToInt32(row["deliveredCount"]);
                        int dCycle = Convert.ToInt32(row["deliveredCycle"]);
                        int dNumber = Convert.ToInt32(row["deliveredNumber"]);
                        return (dCount * dNumber * saleSet.UnitPrice).ToString();
                    }
                }
            }
            else
            {
                Guid goodsId = new Guid(strGoodsId);
                Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
                if (parameter != null)
                {
                    decimal a = Math.Round((parameter.P1 - ((parameter.N * parameter.P1 - parameter.PN) / (parameter.N - 1))), 2);
                    decimal b = Math.Round(((parameter.N * parameter.P1 - parameter.PN) / (parameter.N - 1)), 2);

                    foreach (DataRow row in table.Rows)
                    {
                        if (new Guid((string)row["SetId"]) == setId)
                        {
                            int dCount = Convert.ToInt32(row["deliveredCount"]);
                            int dCycle = Convert.ToInt32(row["deliveredCycle"]);
                            int dNumber = Convert.ToInt32(row["deliveredNumber"]);
                            return Math.Round(
                                (dCount * dCycle * dNumber * a + b) * dCount * dCycle * dNumber, 2).ToString();
                        }
                    }
                }
            }
            return "0";
        }


    }
}